<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品分类ID" prop="goodsId">
        <el-input v-model="dataForm.goodsId" placeholder="商品分类ID"></el-input>
      </el-form-item>
      <el-form-item label="是否收藏或关注" prop="isAttention">
        <el-input v-model="dataForm.isAttention" placeholder="是否收藏或关注"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <!--<el-form-item label="创建即上传时间" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建即上传时间"></el-input>
      </el-form-item>
      <el-form-item label="修改即更新时间" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间"></el-input>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import isInteger from 'lodash/isInteger'
  import moment from 'moment'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodsId: '',
          isAttention: '',
          operator: '',
          createUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          modifyUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          isDeleted: '',
          version: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品分类ID不能为空', trigger: 'blur' }
          ],
          isAttention: [
            { required: true, message: '是否收藏或关注不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建即上传时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '修改即更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/generator/upkcollectgoods/info' + (isInteger(this.dataForm.id) ? `/${this.dataForm.id}` : '')),
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.upkCollectgoods.goodsId
                this.dataForm.isAttention = data.upkCollectgoods.isAttention
                this.dataForm.operator = data.upkCollectgoods.operator
                this.dataForm.createUtc = data.upkCollectgoods.createUtc
                this.dataForm.modifyUtc = data.upkCollectgoods.modifyUtc
                this.dataForm.isDeleted = data.upkCollectgoods.isDeleted
                this.dataForm.version = data.upkCollectgoods.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'goodsId': this.dataForm.goodsId,
              'isAttention': this.dataForm.isAttention,
              'operator': this.dataForm.operator,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? this.$http({
              url: this.$http.adornUrl('/generator/upkcollectgoods/save'),
              method: 'post',
              data: this.$http.adornData(params)
            }) : this.$http({
              url: this.$http.adornUrl('/generator/upkcollectgoods/update'),
              method: 'post',
              data: this.$http.adornData(params)
            })
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
